//
//  CommuViewReplace.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/12/7.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  交流区

import UIKit

import WebKit


class CommuViewReplace : WKBaseViewController,WKBaseDelegate {
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        self.wkDelegate = self
        
        let urlStr = commuArray.lastObject
        
        let url = URL.init(string: urlStr as! String)
        
        DispatchQueue.main.async {
            
            
            //根据是否有网络状态加载断网图片
            StatusBarCheck.shared.networkingStatesFromStatebar { (result) in
                
                if result == "notReachable" {
                    DispatchQueue.main.async {
                        
                        
                        
                        self.urlRequestCache = NSURLRequest.init(url: url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
                        self.webView.load(self.urlRequestCache as URLRequest)
                        
                    }
                } else {
                    
                    DispatchQueue.main.async {
                        
                        self.urlRequestCache = NSURLRequest(url: url!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10)
                        self.webView.load(self.urlRequestCache as URLRequest)
                        
                    }
                    
                }
            }
        }
        
        
    }
    
    
    //拦截操作
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        url = (navigationAction.request.url?.absoluteString)!
        
        
        if navigationAction.navigationType == WKNavigationType.linkActivated || url == "http://\(yuMing)/app/message_list.aspx" || url == "http://\(yuMing)/app/message.aspx" && !self.url.contains("token") {
            
            //如果为本身或者本身携带#的url进行自身刷新。
            
            if url.contains("?") {
                url = url + ("&devtype=1&token=") + (token)
            } else {
                url = url + ("?devtype=1&token=") + (token)
            }
            
            //判断是否有重复的url
            if fwqArray.count > 0 && (fwqArray.lastObject as! String) == self.url {
                self.webView.reload()
                decisionHandler(.allow)
                
                self.view.isUserInteractionEnabled = false
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                    self.view.isUserInteractionEnabled = true
                })
                
                fwqArray.removeLastObject()
                return
            }
            
            
            commuArray.add(url)
            
            aaa(str: url)

            //取消拦截
            decisionHandler(.cancel)
            
            
        } else {
            //取消拦截
            decisionHandler(.allow)
        }
    }
    
    //url---
    func aaa(str : String) -> Void {
        
        let vvv = CommuViewController()
        vvv.url = str
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vvv, animated: true)
    }
}

// MARK:- 实现代理方法
extension CommuViewReplace {
    func server() {
        
        self.navigationController!.popToRootViewController(animated: true)
        
        let urlStr = jlqURL + "?devtype=1&token=" + self.token
        
        let url = URL.init(string: urlStr)
        
        self.urlRequestCache = NSURLRequest.init(url: url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
        self.webView.load(self.urlRequestCache as URLRequest)
    }
}
